﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using nRoute.Components;
using nRoute.Components.Composition;
using nRoute.Components.Messaging;
using nRoute.Navigation.Mapping;
using nRoute.ViewModels;
using nRouteWizard.Wizard.Views;

namespace nRouteWizard.Wizard.ViewModels
{
	[DefineNavigationContent("Content/Pages/WidgetConfiguration/", typeof(WidgetConfigurationView))]
	[MapViewModel(typeof(WidgetConfigurationView))]
	public class WidgetConfigurationViewModel : NavigationViewModelBase
	{
		private const string PAGE_TITLE = "Widget ConfigurationView Wizard";

		private readonly Dictionary<int, string> _settings;
		private readonly IDisposable _channelToken;
		private string _errorMessage;
		private int _currentIndex;

		[ResolveConstructor]
		public WidgetConfigurationViewModel(IChannel<SettingInfo> settingsChannel)
		{
			_channelToken = nRoute.Components.RelayExtensions.Subscribe(settingsChannel, OnSetting);

			base.Title = PAGE_TITLE;
			this.CurrentIndex = 1;

			_settings = new Dictionary<int, string>();

			SetupCommands();
		}

#region Properties

		public string ErrorMessage
		{
			get { return _errorMessage; }
			set
			{
				if (!string.Equals(_errorMessage, value, StringComparison.Ordinal))
				{
					_errorMessage = value;
					NotifyPropertyChanged(() => ErrorMessage);
				}
			}
		}

		public int CurrentIndex
		{
			get { return _currentIndex; }
			set
			{
				if (_currentIndex != value)
				{
					_currentIndex = value;
					NotifyPropertyChanged(() => CurrentIndex);
				}
			}
		}

		public ActionCommand PreviousCommand { get; private set; }

		public ActionCommand NextCommand { get; private set; }

		public ActionCommand FinishCommand { get; private set; }

		public ActionCommand OnCompletedReverseCommand { get; private set; }

#endregion

#region

		protected override void OnClosing(Action<bool> confirmCallback)
		{
			_channelToken.Dispose();

			base.OnClosing(confirmCallback);
		}

#endregion

#region Helpers

		private void OnSetting(SettingInfo settingInfo)
		{
			if (_settings.ContainsKey(settingInfo.SettingIndex))
			{
				_settings[settingInfo.SettingIndex] = settingInfo.Setting;
			}
			else
			{
				_settings.Add(settingInfo.SettingIndex, settingInfo.Setting);
			}
		}

		private void SetupCommands()
		{
			PreviousCommand = new ActionCommand(() => 
			{
				CurrentIndex = (CurrentIndex == 2) ? 1 : 2;
				ErrorMessage = null;
			},
			() => CurrentIndex != 1)									// only executable when not in step1
			.RequeryOnPropertyChanged(this, () => CurrentIndex);		// whenever the CurrentStep property changes, re-query status

			NextCommand = new ActionCommand(() => 
			{
				CurrentIndex = (CurrentIndex == 1) ? 2 : 3;
				ErrorMessage = null;
			},
			() => CurrentIndex != 3)							
			.RequeryOnPropertyChanged(this, () => CurrentIndex);		

			FinishCommand = new ActionCommand(() =>
			{
				ErrorMessage = null;

				// check for errors
				if (!_settings.ContainsKey(1) || string.IsNullOrWhiteSpace(_settings[1]))
				{
					ErrorMessage = "Setting 1 is required";
				}
				else if (!_settings.ContainsKey(3) || string.IsNullOrWhiteSpace(_settings[3]))
				{
					ErrorMessage = "Setting 3 is required";
				}

				// if there no errors
				if (string.IsNullOrWhiteSpace(ErrorMessage))
				{
					// do something with the results
					OnCompletedReverseCommand.Execute();
				}
			});

			OnCompletedReverseCommand = new ActionCommand(() => { });
		}

#endregion

	}
}